/**
 * @Description: 周日历时间工具类
 * @author Ldb
 * @date 2022-10-25 10:47:58
 */
import dayjs, { Dayjs } from 'dayjs'
import { WeekCalendarValue } from '@/components/week-calendar/WeekCalendar.vue'
export interface CalendarWeeks {
  // 总周数
  total: number
  // 每周对应的时间
  value: Array<Array<WeekCalendarValue>>
}
/**
 * 匹配天数日期
 */
export interface MatchDay {
  // 匹配时间
  time: Dayjs
  // 匹配事件
  value?: unknown
}

/**
 * 匹配的交叉日期
 */
export interface CrossDay {
  startTime: Dayjs
  stopTime: Dayjs
  color?: string
  name?: string
  value?: unknown
}
// 组件内数据
export interface CalendarValue {
  // 当前日期(实际日期)
  readonly date: Dayjs
  // 当前日期是当年的第几周
  week?: number
  // 是否补充日期
  isSupplementDate?: boolean
  // 当天日期数据
  data?: any
  // 匹配时间
  matchDay?: Array<MatchDay>
  // 交叉时间
  crossDay?: Array<CrossDay>
}
/**
 * 获取一年内所有周以及周数据数组
 * @param year
 */
export function getWeeksByYears(year = dayjs().year()): CalendarWeeks {
  let value = new Array<Array<CalendarValue>>()
  // 获取本年第一天
  const d = new Date(year, 0, 1)
  // 从周一开始算
  while (d.getDay() != 1) {
    d.setDate(d.getDate() + 1)
  }
  // 获取第二年的第一天
  const to = new Date(year + 1, 0, 1)
  // 从第一周开始遍历
  let total = 1
  for (let from = d; from < to; ) {
    let days: Array<CalendarValue> = []
    days.push({ date: dayjs(from) })
    for (let i = 0; i < 6; i++) {
      // 将每一天加入
      from.setDate(from.getDate() + 1)
      days.push({ date: dayjs(from) })
    }
    // 周末最后一天判断
    if (from < to) {
      from.setDate(from.getDate() + 1)
      days.push({ date: dayjs(from) })
    } else {
      to.setDate(to.getDate() - 1)
      days.push({ date: dayjs(to) })
    }
    days.pop()
    value.push(days)
    total++
  }
  total--
  return <CalendarWeeks>{
    total,
    value,
  }
}

/**
 * 获取某年某月周数和每周的日期(大多数情况都是五周，除了平年28天，刚好是周一)
 * @param data
 */
export function getMonthWeeksByYears(data: Dayjs = dayjs()): CalendarWeeks {
  const weeks = new Array<Array<CalendarValue>>()
  const year = data.year()
  const month = data.month() + 1
  let d = new Date()
  d.setFullYear(year, month - 1, 1)
  //某月的第一天是星期几
  let w1 = d.getDay()
  if (w1 == 0) {
    w1 = 7
  }
  d.setFullYear(year, month, 0)
  //某月的总天数
  let dd = d.getDate()
  let d1
  if (w1 != 1) {
    d1 = 7 - w1 + 2
  } else {
    d1 = 1
  }
  let week_count = Math.ceil((dd - d1 + 1) / 7)
  for (let i = 0; i < week_count; i++) {
    let monday = d1 + i * 7
    let sunday = monday + 6
    let startTime = year + '-' + month + '-' + monday
    let endTime
    if (sunday <= dd) {
      endTime = year + '-' + month + '-' + sunday
    } else {
      d.setFullYear(year, month - 1, sunday)
      endTime = d.getFullYear() + '-' + (d.getMonth() + 1) + '-' + d.getDate()
    }
    // 1、转换为date
    let itemList = new Array<CalendarValue>()
    let item = dayjs(startTime)
    itemList.push({
      date: item,
    })
    for (let j = 0; j < 6; j++) {
      item = item.add(1, 'day')
      itemList.push({
        date: item,
      })
    }
    weeks.push(itemList)
  }
  return <CalendarWeeks>{
    total: week_count,
    value: weeks,
  }
}

/**
 * 格式化日期转Dayjs
 * @param str 格式化日期
 */
export function StrToDayjs(str: string): Dayjs {
  // 解析值
  const dateArr = str.split('-') as unknown as Array<number>
  if (dateArr.length == 3) {
    return dayjs(new Date(dateArr[0], dateArr[1] - 1, dateArr[2]))
  } else {
    throw new Error('只能解析YYYY-MM-DD格式字符串!')
  }
}
/**
 * 格式化时间
 * @param date
 */
export function DateToStr(date: Dayjs, format: string = 'YYYY-MM-DD', hiddenZero = false): string {
  const val = date.format(format)
  return hiddenZero ? (val.indexOf('0') == 0 ? val.substring(1) : val) : val
}
